<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/9/22
 * Time: 9:47
 *订单管理
 */

require('../include/init.inc.php');
require('../../../order_store_procedure.php');
$method = $search = $dn = $page_no = $user_name = $phone = $start_date = $end_date = '';
extract($_REQUEST, EXTR_IF_EXISTS);

if (Common::isGet() && $search == 1) {
    if ($start_date == '' || $end_date == '') {
        OSAdmin::alert('error', '开始时间或者结束时间都不能为空');
        exit;
    }

    $start_time = strtotime($start_date);
    $end_time = strtotime($end_date) + (23 * 3600) + (59 * 60) + 59;

    if ($start_time > $end_time) {
        OSAdmin::alert('error', '开始时间不能小于结束时间');
        exit;
    }

    if (($end_time - $start_time) > 30 * 24 * 3600) {
        OSAdmin::alert('error', '查询周期不能操作30天');
        exit;
    }


    $search_str = " WHERE `status` = 0 AND createtime >= '$start_time' and createtime <= '$end_time'";
    if ($phone != '' && $user_name == '') {
        $search_str .= " AND phone = '$phone'";
    } elseif ($phone == '' && $user_name != '') {
        $search_str .= " AND user_name = '$user_name'";
    } elseif ($phone != '' && $user_name != '') {
        $search_str .= " AND user_name = '$user_name' AND phone = '$phone'";
    }


    //先创建临时表 将数据导入 然后在再临时表中进行查询等操作
//    $db = createTemTable();
//    $allData = getAllData();
//    $sql_str = '';
//    $sign = ',';
//    foreach ($allData as $website => $data) {
//        if (!$data) {
//            unset($allData[$website]);
//        }
//    }
//
//    foreach ($allData as $website => $data) {
//        if ($data) {
//            foreach ($data as $item) {
//
//                $ws = "'" . $website . "'";
//                $order_sn = "'" . $item['order_sn'] . "'";
//                $userid = "'" . $item['userid'] . "'";
//                $shipping_method = "'" . $item['shipping_method'] . "'";
//                $pay_type = "'" . $item['pay_type'] . "'";
//                $user_name = "'" . $item['user_name'] . "'";
//                $shipping_time = "'" . $item['shipping_time'] . "'";
//                $phone = "'" . $item['phone'] . "'";
//                $line = "'" . $item['line'] . "'";
//                $addres = "'" . $item['addres'] . "'";
//                $gender = "'" . $item['gender'] . "'";
//                $email = "'" . $item['email'] . "'";
//                $pay_info = "'" . $item['pay_info'] . "'";
//                $note = "'" . $item['note'] . "'";
//                $status = "'" . $item['status'] . "'";
//                $createtime = "'" . $item['createtime'] . "'";
//                $modifytime = "'" . $item['modifytime'] . "'";
//                $value_str_s = '(' . $ws . ',' . $order_sn . ',' . $userid . ',' . $shipping_method . ',' . $pay_type . ',' . $user_name . ',' . $shipping_time . ',' . $phone . ',' . $line . ',' . $addres . ',' . $gender . ',' . $email . ',' . $pay_info . ',' . $note . ',' . $status . ',' . $createtime . ',' . $modifytime . ')';
//                $sql_str .= $value_str_s . $sign;
//            }
//        }
//    }
//
//
//    $sql_str = substr($sql_str, 0, strlen($sql_str) - 1);
//
//    $sql_exec_str = "replace into `t_tmp_orders` (`web_site`,`order_sn`,`userid`,`shipping_method`,`pay_type`,`user_name`,`shipping_time`,`phone`,`line`,`addres`,`gender`,`email`,`pay_info`,`note`,`status`,`createtime`,`modifytime`) values $sql_str";
//
//
//    $dumpRet = dumpDataToTmp($sql_exec_str);

    //$row_count = AdminOrder::getCountBySearch($search_str);
    $row_count = getCountBySearch($search_str);
    $row_count = $row_count['c'];
    $page_size = PAGE_SIZE;
    $page_no = $page_no < 1 ? 1 : $page_no;

    $total_page = $row_count % $page_size == 0 ? $row_count / $page_size : ceil($row_count / $page_size);
    $total_page = $total_page < 1 ? 1 : $total_page;
    $page_no = $page_no > ($total_page) ? ($total_page) : $page_no;
    $start = ($page_no - 1) * $page_size;

//    $orders = AdminOrder::getOrdersBySearch($start, $page_size, $search_str);
    $orders = getOrdersBySearch($start, $page_size, $search_str);
    foreach ($orders as $ord) {
        $shop_cart = getShopCartBySn($ord['order_sn']);
//        foreach ($shop_cart as $sc) {
//            $newn_order['num'] = $sc['product_nums'];
//            $newn_order['products_color'] = $sc['color'];
//            $newn_order['products_size'] = $sc['size'];
//
//            $product = getProductById($sc['product_id'], $ord['web_site']);
//            $newn_order['total_price'] = $sc['product_nums'] * $product['products_special_price'];
//            $newn_order['products_name'] = $product['products_name'];
//            $newn_order['products_model'] = $product['products_model'];
//            $new_order['prod_infos'][$ord['order_sn']] = $newn_order;
//        }

        $new_order['prod_infos'] = $shop_cart;
        $nameslist = getBlackNameList('user_name');

        foreach ($nameslist as $n) {
            $names[] = $n['user_name'];
        }
        $phonelist = getBlackNameList('phone');
        foreach ($phonelist as $n) {
            $phones[] = $n['phone'];
        }
        $linelist = getBlackNameList('line');
        foreach ($linelist as $n) {
            $lines[] = $n['line'];
        }
        $emaillist = getBlackNameList('email');
        foreach ($emaillist as $n) {
            $emails[] = $n['email'];
        }


        if (($names && in_array($ord['user_name'], $names)) || ($phones && in_array($ord['phone'], $phones)) || ($lines && in_array($ord['line'], $lines)) || ($emails && in_array($ord['email'], $emails))) {
            $new_order['sadOrder'] = "1";
        } else {
            $new_order['sadOrder'] = "0";
        }

        $new_order['pay_type'] = Common::getValue($ord['pay_type'], 'pay_method');
        $payLog = AdminPayLog::getLogByOrderSn($ord['order_sn']);
        if ($ord['pay_type'] == '111') {
            $new_order['pay_info'] = "貨到付款用戶";
        } else {
            if (!$payLog) {
                $new_order['pay_info'] = "支付未成功，漏單用戶";
            } else {
                if (!$payLog['RetStatus'] && !$payLog['RetCode']) {
                    $new_order['pay_info'] = "点击支付按钮之后关闭浏览器导致支付失败";
                } else {
                    if (!$payLog['RetStatus'] && $payLog['RetStatus'] == 1) {
                        $new_order['pay_info'] = "支付成功";
                    } else {
                        $new_order['pay_info'] = $payLog['RetMsg'];
                    }
                }
            }
        }
        $new_order['order_sn'] = $ord['order_sn'];
        $new_order['user_name'] = $ord['user_name'];
        $new_order['phone'] = $ord['phone'];
        $new_order['addres'] = $ord['addres'];
        $new_order['note'] = $ord['note'];
        $new_order['createtime'] = date("Y-m-d H:i:s", $ord['createtime']);
        $kd = getKdInfoBySn($ord['order_sn']);
        $new_order['kd_name'] = $kd['kd_name'];
        if ($kd['isGo'] == 1) {
            $new_order['kd_status'] = '已发货';
        } elseif ($kd['isGo'] == 0) {
            $new_order['kd_status'] = '未发货';
        } else {
            $new_order['kd_status'] = '取消订单';
        }

        $new_orders[] = $new_order;
    }

} else {
    $new_orders = array();
}


$page_html = Pagination::showPager("order_search.php?search=1&phone=$phone&user_name=$user_name&start_date=$start_date&end_date=$end_date", $page_no, PAGE_SIZE, $row_count);

Template::assign('page_no', $page_no);
Template::assign('page_size', PAGE_SIZE);
Template::assign('row_count', $row_count);
Template::assign('page_html', $page_html);
Template::assign('_GET', $_GET);
Template::assign('orders', $new_orders);
Template::display('one_page_shop/order_search.tpl');